package com.example.consumptionrecords.controller;

import com.example.consumptionrecords.entity.http.Response;
import com.example.consumptionrecords.entity.dto.ConsumptionRecordsDTO;
import com.example.consumptionrecords.entity.dto.QueryConsumptionRecordsDTO;
import com.example.consumptionrecords.entity.pojo.User;
import com.example.consumptionrecords.exception.GlobalException;
import com.example.consumptionrecords.service.interfaces.ConsumptionRecordsService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * hhzr9
 * 2024 \ 04 \ 07
 */


@Slf4j
@RestController
@RequestMapping(value = "/cr")
public class ConsumptionRecordsController {

	@Resource
	private ConsumptionRecordsService consumptionRecordsService;

	@PostMapping("/dateQueryConsumptionRecords")
	public ResponseEntity<Response<List<ConsumptionRecordsDTO>>> dateQueryConsumptionRecords(@RequestBody QueryConsumptionRecordsDTO queryConsumptionRecordsVo) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		try {
			log.info("用户：{} 日期查询消费记录", user.getUserPhone());
			List<ConsumptionRecordsDTO> consumptionRecordsDTOS = consumptionRecordsService.queryConsumptionRecords(queryConsumptionRecordsVo, user.getId());
			log.info("用户：{} 日期查询消费记录成功", user.getUserPhone());
			return ResponseEntity.ok(Response.success(consumptionRecordsDTOS));
		}  catch (NullPointerException | GlobalException e) {
			log.error("用户：{} 日期查询消费记录出错", user.getUserPhone(), e);
			return ResponseEntity.ok(Response.error());
		}
	}
}
